vim9script noclear

var trigger = v:true
var revert = {}
var pads = {}

def Resize_pads()
	var vmargin = float2nr(&lines * 0.08)
	var hmargin = (&columns - 80) / 2

	win_execute(pads.t, $"resize {vmargin}")
	win_execute(pads.b, $"resize {vmargin}")
	win_execute(pads.l, $"vertical resize {hmargin}")
	win_execute(pads.r, $"vertical resize {hmargin}")
enddef

def Init_pad(cmd: string): number
	silent execute cmd

	var bufnr = win_getid()
	setlocal buftype=nofile bufhidden=wipe nomodifiable 
	setlocal nobuflisted noswapfile nonu statusline=\ 
	win_gotoid(win_getid(winnr('#')))

	return bufnr
enddef

def ZenOn()
	tab split

	setlocal nonu
	set winwidth=1 laststatus=0 showtabline=0
	set fillchars=vert:\ ,fold:\ ,eob:\ ,lastline:\ 
	setlocal statusline=\ 

	pads.t = Init_pad('topleft new')
	pads.b = Init_pad('botright new')
	pads.l = Init_pad('vertical topleft new')
	pads.r = Init_pad('vertical botright new')

	Resize_pads()
enddef

def ZenOff()
	var save_cursor = getcurpos()

	tabclose
	setpos('.', save_cursor)

	set winwidth=20 laststatus=2 showtabline=1
	set fillchars=vert:│,fold:-,eob:\ ,lastline:@
enddef

export def ZenMode()
	(trigger) ? ZenOn() : ZenOff()
	trigger = !trigger
enddef
